package cn.kgc.jdbc.dao;

import cn.kgc.jdbc.entity.User;
import cn.kgc.jdbc.util.DruidUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @author: mosin
 * @version: v1.0  2022/8/26
 */
public class UserDaoImpl implements UserDao {

    public List<User> findAll(){
        QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());
        String sql = "select *  from user";
        try {
            List<User> users = queryRunner.query(sql, new BeanListHandler<User>(User.class));
            return users;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return  null;
    }


    public Integer  addUser(User user){

        QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());
        String sql = " insert into user(username,password)values(?,?)";
        try {
            int line = queryRunner.update(sql, user.getUsername(), user.getPassword());
            return line;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public Integer updateUser(User user) {
        return null;
    }

    @Override
    public Integer deleteUser(Integer id) {
        return null;
    }

}
